using System;
using System.Collections.Generic;
using System.Text;

using NBear.Common;

namespace Sample_Entity_Configuration_By_Entity_Configurator
{
    [Table(IsContract = true)]
    public interface IdentableEntity : IEntity
    {
        [PrimaryKey]
        int Id { get; set; }
        string Name { get; set; }
    }

    [Table(IsContract = true)]
    public interface Loginable : IEntity
    {
        string LoginId { get; set; }
    }

    [Table(IsContract = true)]
    public interface PasswordLoginable : Loginable
    {
        string Password { get; set; }
    }

    [Table(IsContract = true)]
    public interface PrivilegeAssignable : IEntity
    {
        int PrivilegeOwnerId { get; set; }
    }

    [Table("User", UpdateTableName = "User", IsCascadeUpdate = true)]
    public interface User : IdentableEntity, PrivilegeAssignable
    {
    }

    [Table("select User.*, LocalUser.LoginId, LocalUser.Password from LocalUser inner join User on LocalUser.Id = User.Id", UpdateTableName = "LocalUser", IsView = true, IsCascadeUpdate = true)]
    public interface LocalUser : PasswordLoginable, User
    {
    }

    [Table("select User.*, AgentUser.LoginId from AgentUser inner join User on AgentUser.Id = User.Id", UpdateTableName = "AgentUser", IsView = true, IsCascadeUpdate = true)]
    public interface AgentUser : Loginable, User
    {
    }

    [Table("select User.* from GhostUser inner join User on GhostUser.Id = User.Id", UpdateTableName = "GhostUser", IsView = true, IsCascadeUpdate = true)]
    public interface GhostUser : User
    {
    }

    [Table("UserGroup", UpdateTableName = "UserGroup", IsCascadeUpdate = true)]
    public interface UserGroup : IdentableEntity, PrivilegeAssignable
    {
        string Comment { get; set; }
    }

    public interface IdFactory : IEntity
    {
        int NextId { get; set; }
    }

    public interface EntityWithCustomProperty : IEntity
    {
        [PrimaryKey]
        int ID { get; }
        string Name { get; set; }

        string XmlServerConfig { get; set; }

        [CustomProperty("p1", "p2")]
        SampleCustomPropertyType SampleProperty { get; }

        string XmlContactConfig { get; set; }
    }

    [Serializable]
    public class SampleServerConfig
    {
        public string ServerAddress;
        public int ServerPort;
    }

    [Serializable]
    public class SampleContactConfig
    {
        public string WorkPhone;
        public string HomePhone;
    }

    public class SampleCustomPropertyType : CustomPropertyType
    {
        public SampleServerConfig ServerConfig
        {
            get
            {
                return SerializeHelper.Deserialize<SampleServerConfig>(typeof(SampleServerConfig), (string)keyValues["XmlServerConfig"]);
            }
        }

        public SampleContactConfig ContactConfig
        {
            get
            {
                return SerializeHelper.Deserialize<SampleContactConfig>(typeof(SampleContactConfig), (string)keyValues["XmlContactConfig"]);
            }
        }

        public SampleCustomPropertyType(KeyValueCollection keyValues, string[] parameters)
            : base(keyValues, parameters)
        {
        }
    }
}
